import React, { Component } from 'react';
import { Table, Popconfirm, Image, message, Button, Modal, Form, DatePicker, Select, Input, InputNumber } from 'antd';
import axios from 'axios';
import { GET_ALLYOUACTIVITY, GET_DELHUODONG } from '../../utils/api'

class Huodongguanli extends Component {
    state = {
        tableData: [],
    }
    xiugai = (row) => () => {
        console.log(row)
    }
    shanchu = (id) => () => {
        axios({
            url: GET_DELHUODONG,
            method: 'get',
            params: {
                activity_id: id
            }
        }).then(data => {
            if (data.data.code === 200) {
                message.success('删除成功~', 3);
                this.getData()
            } else {
                message.error('删除失败！', 3);
            }
        })
    }
    // 获取数据
    getData() {
        axios({
            url: GET_ALLYOUACTIVITY,
            method: 'get',
            params: {}
        }).then(data => {
            this.setState({
                tableData: data.data.data
            })
        })
    }
    componentDidMount() {
        this.getData()
    }
    render() {
        const tableData = this.state.tableData
        const columns = [
            {
                title: '活动编号',
                dataIndex: 'activity_id',
                key: 'activity_id',
                sorter: (a, b) => a.activity_id - b.activity_id,

            },
            {
                title: '活动名称',
                dataIndex: 'activity_name',
                key: 'activity_name',
            },
            {
                title: '活动图片',
                render: (row) =>
                    <Image
                        width={200}
                        src={"http://192.168.2.146:8766" + row.activity_img}
                    />
            },
            {
                title: '活动规则',
                dataIndex: 'activity_rules',
                key: 'activity_rules',
            },
            {
                title: '活动开始时间',
                dataIndex: 'activity_start_time',
                key: 'activity_start_time',
            },
            {
                title: '活动结束时间',
                dataIndex: 'activity_end_time',
                key: 'activity_end_time',
            },
            {
                title: '操作',
                width: '200px',
                align: 'center',
                render: (row) =>
                    <div>
                        <Popconfirm placement="left" title='是否确定删除该条数据？' onConfirm={this.shanchu(row.activity_id)} okText="确定" cancelText="取消">
                            <Button style={{ marginRight: '10px' }} danger>删除</Button>
                        </Popconfirm>
                        <Button type='primary' onClick={this.xiugai(row)}>修改</Button>
                    </div>
            }
        ];
        return (
            <div>
                <Button style={{ margin: '20px' }} type="primary"> 新增优惠券</Button>

                {/* 表格 */}
                <Table
                    pagination={{ position: ['bottomCenter'], pageSize: 6 }}
                    columns={columns}
                    dataSource={tableData}
                    rowKey={(record) => `complete${record.activity_id}`}
                />
            </div>
        );
    }
}

export default Huodongguanli;